package org.dreamfly.core.util;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-12-2
 * Time: 20:03:47
 * To change this template use File | Settings | File Templates.
 */

import org.apache.commons.net.ftp.FTPClient;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.List;

public class RemoteUpload {

/**
    * @param args
    */
public static void main(String[] args) throws Exception{
      RemoteUpload myUpFile = new RemoteUpload();
      //myUpFile.upMyfile("localhost", "gu", "19910410", "/temp/","G:/0145.jpg");

      myUpFile.upFileByConfigXml(null, null);

     }
public int upMyfile(String ip,String userId,String pwd,String upDir,String localFileDir)
{
    int prot = 21;
    FTPClient ftpClient = new FTPClient();
    try {
     // 连接主机
     ftpClient.connect(ip, prot);
     // 连接成功后用指定帐号密码登录到该主机
     System.out.println("连接ftp服务器:[" + ip + "]成功!");
     System.out.println("系统正在登录......");
     //System.out.println(ftpClient.login(userId, pwd));
     if(ftpClient.login(userId, pwd))
     {
      System.out.println("系统登录成功！");
     }else
     {
      System.out.println("用户名或密码错误！");
      return 0;
     }
     // 将工作目录切换到指定目录
     //ftpClient.setControlEncoding("GBK");
     if(ftpClient.changeWorkingDirectory(upDir))
     {
      System.out.println("切换到指定目录成功！");
     }else
     {
      System.out.println("切换到指定目录失败！请检查指定上传目录路径是否正确!");
      return 0;
     }
        
     //ftpClient.changeWorkingDirectory("/home/tssa");
     // 无此句，传输非txt文件会变大
     ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
     ftpClient.enterLocalPassiveMode();
     // 指定要上传的文件目录,通过传入的localFileDir参数值获得
     File file1 = new File(localFileDir);
     FileInputStream input = new FileInputStream(file1);
     // 上传本地指定目录下的文件

     if(ftpClient.storeFile(file1.getName(),input))
     {
      System.out.println("指定文件上传成功！");
     }else
     {
      System.out.println("指定文件上传失败！此目录下该用户无权上传文件！");
      return 0;
     }

     // 上传成功后退出系统
     ftpClient.logout();
     System.out.println("任务完成，退出登录！");
     return 1;

    } catch (SocketException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return 0;
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return 0;
    }
}


public static void upFileByConfigXml(String configPath, String fileDir)
{
    RemoteUpload myUpFile = new RemoteUpload();
    try
    {
      SAXBuilder sb = new SAXBuilder();
      // 读取工程相对路径下的xml配置文件
      Document doc = sb.build(configPath);
      Element root = doc.getRootElement();
      Element elms = null;
      /*Element elms1 = null;

      // 取得本地要上传文件的路径
      List list2 = root.getChildren("localFileDir");
      elms1 = (Element)list2.get(0);
      String fileDir = elms1.getChildText("fileDir");*/

      // 取得各个服务器的配置，并将配置作为参数传给方法upMyfile
      List list1 = root.getChildren("serverConfig");
      for(int i=0; i< list1.size(); i++)
      {
       elms = (Element)list1.get(i);
       System.out.println("*************************************************************");
       System.out.println("------------------------["+ elms.getChildText("serverName")+ "]    UpCount:" +i +"---------------------");
       System.out.println("*************************************************************");
       // 开始上传到配置文件中指定的server
       int upFileFlag = myUpFile.upMyfile(elms.getChildText("ip"), elms.getChildText("userId"), elms.getChildText("pwd"), elms.getChildText("upDir"),fileDir);
       if(upFileFlag==1)
       {
        System.out.println(elms.getChildText("serverName")+"("+elms.getChildText("ip")+")"+"：上传文件成功！");
       }else
       {
        System.out.println(elms.getChildText("serverName")+"("+elms.getChildText("ip")+")"+"：上传文件失败！");
       }
      }
    }
    catch(Exception e)
    {
     e.printStackTrace();
    }
}

}
